package com.m1905hd.user.utils

import com.aliyun.oss.ClientException
import com.aliyun.oss.OSSClient
import com.aliyun.oss.OSSException
import com.aliyun.oss.model.ObjectMetadata
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.input.BoundedInputStream
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 *
 * @author weiwei.huang
 * @email hww@xiaocong.tv
 * @date 2014/7/24
 */
class AliOSSUtil {


    static final Logger log = LoggerFactory.getLogger(AliOSSUtil.class);


    private static final String BUCKET = "hdbox";

    public static final long PART_SIZE = 20 * 1024 * 1024L; // 每个Part的大小，最小为5MB

    private static final int CONCURRENCIES = 4; // 上传Part的并发线程数。

    public static void uploadFile(OSSClient client, String key, File file) {
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(file.length());
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
        InputStream input = new FileInputStream(file);
        client.putObject(BUCKET, key, input, objectMeta);
    }

    // 上传文件
    public static void uploadFile(OSSClient client, String bucketName, String key, File file)
            throws OSSException, ClientException, FileNotFoundException {

        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(file.length());
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");

        InputStream input = new FileInputStream(file);
        client.putObject(bucketName, key, input, objectMeta);
    }
    //计算MD5
    public static String calcPartMD5(File file, long skipBytes, long partSize) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.skip(skipBytes);

        // 计算校验和
        BoundedInputStream bis = new BoundedInputStream(fileInputStream, partSize);
        String partMD5 = DigestUtils.md5Hex(bis).toUpperCase();

        return partMD5;
    }

    //md5文件写入oss
    public static void uploadDescFile(OSSClient client, String key, String result, long length) {
        log.info("准备上传到阿里云OSS中的描述文件名为:" + key);
        InputStream input = null;
        String bucket ="hdapk";
        try {
            input = new ByteArrayInputStream(result.getBytes("UTF-8"));
            if (input != null) {
                uploadFileDesc(client, bucket, key, input, length, null);
            }
        } catch (UnsupportedEncodingException e) {
            log.error("不支持文件编码方式:", e);
        }
    }

    // 将输入流的内容写入阿里云oss文件中
    private
    static void uploadFileDesc(OSSClient client, String bucketName, String key, InputStream input, Long length, String contentType)
            throws OSSException, ClientException, FileNotFoundException {
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(length);
        client.putObject(bucketName, key, input, objectMeta);
    }

    //上传APK
    public static void uploadApk(OSSClient client, String key, File file) {
        String BUCKET ="hdapk"
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(file.length());
        // 可以在metadata中标记文件类型
        InputStream input = new FileInputStream(file);
        client.putObject(BUCKET, key, input, objectMeta);
    }


    static main(a) {
        OSSClient client = new OSSClient("ggyq1JXvSaDTtutm", "9TF9aLCwwhzs20BXGWL7CqqdtVnjG2")

        uploadFile(client, "ihww", "iyuanrong/1.abc", "/Users/ihww/Desktop/服务器组报告.ppt")
    }
}
